//
//  EsTabBarController.m
//  Estate
//
//  Created by heyong on 16/5/25.
//  Copyright © 2016年 ttsx. All rights reserved.
//

#import "EditImageView.h"
#import "ShiGFWorkViewController.h"
#import "SGFGtasksViewController.h"
#import "EsTabBarController.h"
#import "CameraViewController.h"
#import "IndexViewController.h"
#import "PersonViewController.h"
#import "UIImage+ColorImage.h"
#import "LSUtility.h"
//监理
#import "JianLWorkViewController.h"
#import "JLGtasksViewController.h"
//总监
#import "ZongJWorkViewController.h"
#import "ZJGtasksViewController.h"
//工程巡检
#import "EngLeaderIndexController.h"
#import "InspectorViewController.h"
//甲方
#import "BuildWorkController.h"
#import "BuildGtasksController.h"
// 检测方
#import "TestingViewController.h"

// 其他
#import "testViewController.h"

@interface EsTabBarController ()<UIImagePickerControllerDelegate>
{
    IndexViewController *indexVC;
}
@property (strong ,nonatomic)FMDatabase *db;
/**注释 */
@property (nonatomic,strong) EditImageView *editImageView;
@end

@implementation EsTabBarController

- (FMDatabase *)db{
    if(_db==nil){
        _db=[FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    }
    return _db;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 定制外观
    [[UINavigationBar appearance] setShadowImage:[UIImage imageWithColor:[UIColor whiteColor]]];
    
    [[UITabBar appearance] setShadowImage:[[UIImage alloc]init]];
    //设置背景颜色
    [[UITabBar appearance] setBackgroundImage:[UIImage imageWithColor:[TTTheme defaultManger].tabBarColor]];
    
    // 自定义tabBar
    // 1. 先创建一个自定义tabBar
    EsTabBar *tabBar = [[EsTabBar alloc] initWithFrame:self.tabBar.frame];
    // 2. 利用KVC把readonly权限改过来
    [self setValue:tabBar forKeyPath:@"tabBar"];
    [tabBar.plusButton addTarget:self action:@selector(Camera) forControlEvents:UIControlEventTouchUpInside];

}

#pragma -mark设置角色
- (void) setProId:(NSString *)proId setProType:(NSString *)proType andUserId:(NSString *)userId andRole:(NSString *)role{
    [self.db open];

    //永久化userId
    DEF_PERSISTENT_SET_OBJECT(userId, @"userid");
    //永久化项目id
    DEF_PERSISTENT_SET_OBJECT(proId, @"proid");
    
    DEF_PERSISTENT_SET_OBJECT(proType, @"proType");

    //查询项目
    NSString *sqlPro=[NSString stringWithFormat:@"SELECT * FROM PROJECT WHERE ID = '%@'",proId];
    FMResultSet *result = [self.db executeQuery:sqlPro];
    NSString *proName;
    //项目名
    while ([result next]) {
        proName= [result stringForColumn:@"PROJECT_NAME"];
    }
    [self.db close];
    //永久化项目名称
    DEF_PERSISTENT_SET_OBJECT(proName, @"proname");
    
    //设置角色 永久化角色
    //总监
    if([role isEqualToString:@"USER_01"]){
        _flag=@"Director";
        DEF_PERSISTENT_SET_OBJECT(role, @"role");
    }//监理
    else if ([role isEqualToString:@"USER_03"]){
        _flag=@"superior";
        DEF_PERSISTENT_SET_OBJECT(role, @"role");
    }//专监理
    else if ([role isEqualToString:@"USER_07"]){
        _flag=@"superior";
        DEF_PERSISTENT_SET_OBJECT(role, @"role");
    //施工方
    }else if([role isEqualToString:@"CONSTRACT_01"]||[role isEqualToString:@"CONSTRACT_02"]||[role isEqualToString:@"CONSTRACT_03"]||[role isEqualToString:@"CONSTRACT_04"]||[role isEqualToString:@"CONSTRACT_05"]||[role isEqualToString:@"CONSTRACT_06"]||[role isEqualToString:@"CONSTRACT_07"])
    {
        _flag=@"shigongfang";
         DEF_PERSISTENT_SET_OBJECT(role, @"role");
    // 领导
    } else if ([role isEqualToString:@"LEADER_01"]||[role isEqualToString:@"PROJECT_SUB_LEADER"]){
        _flag=@"leader";
        DEF_PERSISTENT_SET_OBJECT(role, @"role");
    // 巡检组长
    } else if ([role isEqualToString:@"1"]){
        _flag=@"teamleader";
        DEF_PERSISTENT_SET_OBJECT(role, @"role");
    // 巡检员
    } else if ([role isEqualToString:@"2"]){
        _flag=@"inspector";
        DEF_PERSISTENT_SET_OBJECT(role, @"role");
    //甲方 建设单位
    } else if ([role isEqualToString:@"BUILD_01"]||[role isEqualToString:@"BUILD_02"]){
        _flag=@"build";
        DEF_PERSISTENT_SET_OBJECT(role, @"role");
        
    // 检测方
    } else if ([role isEqualToString:@"CONSTRACT_08"]) {
        _flag = @"testing";
        DEF_PERSISTENT_SET_OBJECT(role, @"role");
    }

    
    [self setupChildViewController];
}

#pragma mark - 设置样式
+(void)initialize{
    UITabBarItem *tabbarItems=[UITabBarItem appearance];
    //设置选中item文字颜色
    NSDictionary *dic=@{NSForegroundColorAttributeName:[TTTheme defaultManger].defaultColor};
    [tabbarItems setTitleTextAttributes:dic forState:UIControlStateSelected];
    //设置未选中item文字颜色
    NSDictionary *dic1=@{NSForegroundColorAttributeName:[UIColor colorWithRed:0.27 green:0.28 blue:0.28 alpha:1.00]};
    [tabbarItems setTitleTextAttributes:dic1 forState:UIControlStateNormal];
}

#pragma mark-添加视图控制器
- (void)setupChildViewController{
    //领导
    if([_flag isEqualToString:@"leader"]){
        
    
    }else{
        // 首页
        indexVC = [[IndexViewController alloc] init];
        [self setUpChildViewController:indexVC image:[UIImage imageNamed:@"tabbar_home"] selectedImage:[UIImage imageNamed:@"tabbar_home_hl"] title:@"首页"];
        //监理
        if([_flag isEqualToString:@"superior"])
        {
            //:!!!!!!工作
            JianLWorkViewController *workVC = [[JianLWorkViewController alloc] init];
            [self setUpChildViewController:workVC image:[UIImage imageNamed:@"tabbar_activity"] selectedImage:[UIImage imageNamed:@"tabbar_activity_hl"] title:@"工作"];
            //:!!!!!!待办
            JLGtasksViewController *forumVC = [[JLGtasksViewController alloc] init];
            [self setUpChildViewController:forumVC image:[UIImage imageNamed:@"tabbar_club"] selectedImage:[UIImage imageNamed:@"tabbar_club_hl"] title:@"待办"];
            
        }
        else if ([_flag isEqualToString:@"Director"])
        //总监
        {  //:!!!!!!总监--工作
            ZongJWorkViewController *workVC = [[ZongJWorkViewController alloc] init];
            [self setUpChildViewController:workVC image:[UIImage imageNamed:@"tabbar_activity"] selectedImage:[UIImage imageNamed:@"tabbar_activity_hl"] title:@"工作"];
            //:!!!!!!待办
            ZJGtasksViewController *forumVC = [[ZJGtasksViewController alloc] init];
            [self setUpChildViewController:forumVC image:[UIImage imageNamed:@"tabbar_club"] selectedImage:[UIImage imageNamed:@"tabbar_club_hl"] title:@"待办"];
        }
        //施工方
        else if([_flag isEqualToString:@"shigongfang"])
        {  //:!!!!!!施工方 -- 工作
            ShiGFWorkViewController *workVC = [[ShiGFWorkViewController alloc] init];
            [self setUpChildViewController:workVC image:[UIImage imageNamed:@"tabbar_activity"] selectedImage:[UIImage imageNamed:@"tabbar_activity_hl"] title:@"工作"];
//            //:!!!!!!待办
            SGFGtasksViewController *forumVC = [[SGFGtasksViewController alloc] init];
            [self setUpChildViewController:forumVC image:[UIImage imageNamed:@"tabbar_club"] selectedImage:[UIImage imageNamed:@"tabbar_club_hl"] title:@"待办"];
     
        }
        
        //巡检组长
        else if([_flag isEqualToString:@"teamleader"]){
            //工程巡检
            EngLeaderIndexController *engLeaderVC=[[EngLeaderIndexController alloc]init];
            [self setUpChildViewController:engLeaderVC image:[UIImage imageNamed:@"gongchengxunjian1"] selectedImage:[UIImage imageNamed:@"gongchengxunjian"] title:@"巡检"];
            //工作绩效
            testViewController *testVC1=[[testViewController alloc]init];
            [self setUpChildViewController:testVC1 image:[UIImage imageNamed:@"tabbar_club"] selectedImage:[UIImage imageNamed:@"tabbar_club_hl"] title:@"待办"];
        //巡检员
        } else if ([_flag isEqualToString:@"inspector"]) {
            // 工程巡检
            InspectorViewController *inspectorVC = [[InspectorViewController alloc] init];
            [self setUpChildViewController:inspectorVC image:[UIImage imageNamed:@"gongchengxunjian1"] selectedImage:[UIImage imageNamed:@"gongchengxunjian"] title:@"巡检"];
            
            //工作绩效
            testViewController *testVC1=[[testViewController alloc]init];
            [self setUpChildViewController:testVC1 image:[UIImage imageNamed:@"tabbar_club"] selectedImage:[UIImage imageNamed:@"tabbar_club_hl"] title:@"待办"];
        //甲方
        } else if ([_flag isEqualToString:@"build"]){
            //工作
            BuildWorkController *buildWork=[[BuildWorkController alloc]init];
            [self setUpChildViewController:buildWork image:[UIImage imageNamed:@"tabbar_activity"] selectedImage:[UIImage imageNamed:@"tabbar_activity_hl"] title:@"工作"];
            //代办
            BuildGtasksController *BuildGtasks=[[BuildGtasksController alloc]init];
            [self setUpChildViewController:BuildGtasks image:[UIImage imageNamed:@"tabbar_club"] selectedImage:[UIImage imageNamed:@"tabbar_club_hl"] title:@"待办"];
            
        // 检测方
        } else if ([_flag isEqualToString:@"testing"]) {
            // 见证取样
            TestingViewController *testingVC = [[TestingViewController alloc] init];
            [self setUpChildViewController:testingVC image:[UIImage imageNamed:@"jiance"] selectedImage:[UIImage imageNamed:@"jiance1"] title:@"检测"];
            
            // 建设中
            testViewController *testVC1=[[testViewController alloc]init];
            [self setUpChildViewController:testVC1 image:[UIImage imageNamed:@"tabbar_club"] selectedImage:[UIImage imageNamed:@"tabbar_club_hl"] title:@"待办"];
        }
        //创建PDF文件夹
        NSFileManager *fileManager = [[NSFileManager alloc] init];
        NSString *createPath =DEF_FILE_PDF_PATH;
        // 判断文件夹是否存在，如果不存在，则创建
        if (![[NSFileManager defaultManager] fileExistsAtPath:createPath]) {
            [fileManager createDirectoryAtPath:createPath withIntermediateDirectories:YES attributes:nil error:nil];
        } else {
            NSLog(@"FileDir is exists.");
        }
    }
    // 个人
    PersonViewController *mineVC = [[PersonViewController alloc] init];
    [self setUpChildViewController:mineVC image:[UIImage imageNamed:@"tabar_profile"] selectedImage:[UIImage imageNamed:@"tabbar_profile_hl"] title:@"个人"];
}

- (void)setUpChildViewController:(UIViewController *)vc image:(UIImage *)image selectedImage:(UIImage *)selectedImage title:(NSString *)title{
    //关闭图片渲染
    image=[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    selectedImage=[selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    if(![vc isKindOfClass:NSClassFromString(@"MapIndexViewController")]){
        vc.view.backgroundColor=[UIColor colorWithRed:0.95 green:0.96 blue:0.96 alpha:1.00];
    }
    vc.title = title;
    vc.tabBarItem.image = image;
    vc.tabBarItem.selectedImage = selectedImage;
    EsNavigationController *nav = [[EsNavigationController alloc] initWithRootViewController:vc];
    [self addChildViewController:nav];
}

#pragma mark - 调用系统相机
- (void)Camera{
    //判断是否支持相机
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
        //类型为相机 UIImagePickerControllerSourceTypePhotoLibrary类型为相册
        NSUInteger sourceType=UIImagePickerControllerSourceTypeCamera;
        //跳转至对应界面
        UIImagePickerController *imagePickerCtr=[[UIImagePickerController alloc]init];
        imagePickerCtr. delegate=self;
        imagePickerCtr.sourceType=sourceType;
        [self presentViewController:imagePickerCtr animated:YES completion:nil];
        
    }else{
        NSLog(@"该设备不支持相机");
    }
}

// 使用照片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    
    UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];
    self.editImageView = [[EditImageView alloc]initWithImage:image];
    WEAKSELF
    self.editImageView.completeBlock = ^(NSString *path) {
        [weakSelf dismissViewControllerAnimated:YES completion:nil];
        //关闭相册界面
        [UIView animateWithDuration:0.01 animations:^{
            [picker dismissViewControllerAnimated:NO completion:nil];
        } completion:^(BOOL finished){
            //重新打开相机
            [weakSelf presentViewController:picker animated:YES completion:nil];
        }];
    };
    
    
    
    
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{

    [picker dismissViewControllerAnimated:YES completion:nil];

}

@end
